print("时间##################################################start")
from datetime import datetime, timedelta, timezone

now = datetime.now()  # 获取当前datetime
print(now)
print(type(now))

dt = datetime(2015, 4, 19, 12, 20, 9)  # 用指定日期时间创建datetime
print(dt)
print(dt.timestamp())  # 把datetime转换为时间戳
print(datetime.fromtimestamp(dt.timestamp()))  # 时间戳转换为datetime
print(datetime.utcfromtimestamp(dt.timestamp()))  # 时间戳转换为datetime 设置时区为UTC
cday = datetime.strptime('2015-6-1 18:19:59', '%Y-%m-%d %H:%M:%S')  # str转换为datetime
print(cday)
print(now.strftime('%a, %b %d %H:%M'))  # datetime转换为str
print(now.strftime('%Y-%m-%d %H:%M:%S'))  # datetime转换为str
# 时间的加减需要引入timedelta
print(now + timedelta(hours=10))  # 时间加10个小时
print(now - timedelta(days=1))  # 时间-1天
print(now + timedelta(days=2, hours=12))  # 时间加2天12个小时

# 本地时间转换为UTC时间
tz_utc_8 = timezone(timedelta(hours=8))  # 创建时区UTC+8:00
dt = now.replace(tzinfo=tz_utc_8)  # 强制设置为UTC+8:00
print(dt)

# 时区转换
utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc)  # 拿到UTC时间，并强制设置时区为UTC+0:00
print(utc_dt)
bj_dt = utc_dt.astimezone(timezone(timedelta(hours=8)))  # astimezone()将转换时区为北京时间
print(bj_dt)
tokyo_dt = utc_dt.astimezone(timezone(timedelta(hours=9)))  # astimezone()将转换时区为东京时间
print(tokyo_dt)
tokyo_dt2 = bj_dt.astimezone(timezone(timedelta(hours=9)))  # astimezone()将bj_dt转换时区为东京时间
print(tokyo_dt2)
print("时间##################################################end\n")
